import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QAction, QMenu, 
                             QToolBar, QStatusBar, QTextEdit, QDockWidget, 
                             QListWidget, QLabel, QMessageBox, QVBoxLayout, 
                             QWidget, QPushButton)
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt

class ProfessionalMainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.init_ui()
        
    def init_ui(self):
        self.setWindowTitle("专业应用程序")
        self.setGeometry(100, 100, 1000, 700)
        
        # 初始化所有组件
        self.create_central_widget()
        self.create_actions()
        self.create_menus()
        self.create_toolbars()
        self.create_statusbar()
        self.create_docks()
        
        # 连接信号槽
        self.connect_signals()
        
    def create_central_widget(self):
        """创建中心部件"""
        self.text_edit = QTextEdit()
        self.text_edit.textChanged.connect(self.on_text_changed)
        self.setCentralWidget(self.text_edit)
        
    def create_actions(self):
        """创建所有动作"""
        # 文件操作
        self.new_action = QAction("新建", self)
        self.new_action.setShortcut("Ctrl+N")
        
        self.open_action = QAction("打开", self)
        self.open_action.setShortcut("Ctrl+O")
        
        # 编辑操作
        self.copy_action = QAction("复制", self)
        self.copy_action.setShortcut("Ctrl+C")
        
    def create_menus(self):
        """创建菜单系统"""
        menubar = self.menuBar()
        
        # 文件菜单
        file_menu = menubar.addMenu("文件")
        file_menu.addAction(self.new_action)
        file_menu.addAction(self.open_action)
        
        # 视图菜单 - 控制界面元素显示
        view_menu = menubar.addMenu("视图")
        self.toolbar_action = view_menu.addAction("工具栏")
        self.toolbar_action.setCheckable(True)
        self.toolbar_action.setChecked(True)
        
    def create_toolbars(self):
        """创建工具栏系统"""
        # 主工具栏
        self.main_toolbar = QToolBar("主工具栏")
        self.main_toolbar.addAction(self.new_action)
        self.main_toolbar.addAction(self.open_action)
        self.addToolBar(Qt.TopToolBarArea, self.main_toolbar)
        
        # 格式工具栏
        self.format_toolbar = QToolBar("格式工具栏")
        self.addToolBar(Qt.TopToolBarArea, self.format_toolbar)
        
    def create_statusbar(self):
        """创建状态栏"""
        self.statusBar().showMessage("就绪")
        
        # 永久状态显示
        self.word_count_label = QLabel("字数: 0")
        self.statusBar().addPermanentWidget(self.word_count_label)
        
    def create_docks(self):
        """创建停靠窗口"""
        # 导航面板
        self.nav_dock = QDockWidget("导航", self)
        nav_list = QListWidget()
        nav_list.addItems(["文档结构", "书签","注释"])
        self.nav_dock.setWidget(nav_list)
        self.addDockWidget(Qt.LeftDockWidgetArea, self.nav_dock)
        
    def connect_signals(self):
        """连接所有信号槽"""
        self.toolbar_action.toggled.connect(self.main_toolbar.setVisible)
        
    def on_text_changed(self):
        """文本改变时的处理"""
        text = self.text_edit.toPlainText()
        word_count = len(text.split())
        self.word_count_label.setText(f"字数: {word_count}")
        
    def closeEvent(self, event):
        """关闭事件处理"""
        reply = QMessageBox.question(
            self, "确认退出",
            "确定要退出应用程序吗？",
            QMessageBox.Yes | QMessageBox.No,
            QMessageBox.No
        )
        
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = ProfessionalMainWindow()
    window.show()
    sys.exit(app.exec_())
